<template>
  <header class="header">
    <div class="container">
      <div class="header-content">
        <!-- Logo -->
        <div class="logo" @click="$router.push('/')" style="cursor: pointer;">
          <i class="fas fa-home logo-icon"></i>
          <span>视分享商城</span>
        </div>

        <!-- 搜索框 -->
        <div class="search-box">
          <input 
            type="text" 
            placeholder="搜索商品..." 
            class="search-input" 
            v-model="searchQuery" 
            @keyup.enter="performSearch"
          >
          <button class="search-btn" @click="performSearch">
            <i class="fas fa-search"></i>
          </button>
        </div>

        <!-- 右侧操作区 -->
        <div class="header-actions">
          <!-- 通知图标 -->
          <div class="notification-icon" @click="showNotificationModal = true">
            <i class="fas fa-bell"></i>
            <span class="notification-badge">1</span>
          </div>

          <!-- 用户菜单 -->
          <div class="user-menu" @click="showUserDropdown = !showUserDropdown">
            <div class="user-avatar">
              <i class="fas fa-user"></i>
            </div>
            <div class="user-dropdown" v-if="showUserDropdown">
              <a @click="navigateTo('/register')" class="dropdown-item">
                <i class="fas fa-user-plus"></i>
                注册
              </a>
              <a @click="navigateTo('/login')" class="dropdown-item">
                <i class="fas fa-sign-in-alt"></i>
                登录
              </a>
              <a @click="navigateTo('/orders')" class="dropdown-item">
                <i class="fas fa-clipboard-list"></i>
                订单
              </a>
              <a @click="navigateTo('/settings')" class="dropdown-item">
                <i class="fas fa-cog"></i>
                设置
              </a>
            </div>
          </div>

          <!-- 订单按钮 -->
          <button class="btn btn-secondary order-btn" @click="navigateTo('/orders')">
            <i class="fas fa-clipboard-list"></i>
            订单
          </button>
        </div>
      </div>
    </div>

    <!-- 通知模态框 -->
    <Modal 
      :is-open="showNotificationModal" 
      title="消息通知"
      @close="showNotificationModal = false"
    >
      <div class="notification-content">
        <div class="notification-item">
          <i class="fas fa-bell notification-icon"></i>
          <div class="notification-text">
            <p>您有1条新消息</p>
            <span class="notification-time">刚刚</span>
          </div>
        </div>
      </div>
    </Modal>
  </header>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue'
import { useRouter } from 'vue-router'
import Modal from './Modal.vue'

const router = useRouter()
const searchQuery = ref('')
const showUserDropdown = ref(false)
const showNotificationModal = ref(false)

// 搜索功能
const performSearch = () => {
  if (searchQuery.value.trim()) {
    router.push(`/search-results?q=${encodeURIComponent(searchQuery.value)}`)
  }
}

// 导航功能
const navigateTo = (path: string) => {
  router.push(path)
  showUserDropdown.value = false
}

// 点击外部关闭下拉菜单
const handleClickOutside = (event: Event) => {
  const target = event.target as HTMLElement
  if (!target.closest('.user-menu')) {
    showUserDropdown.value = false
  }
}

// 监听点击事件
onMounted(() => {
  document.addEventListener('click', handleClickOutside)
})

onUnmounted(() => {
  document.removeEventListener('click', handleClickOutside)
})
</script>

<style scoped>
.header {
  background: white;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  position: sticky;
  top: 0;
  z-index: 1000;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 60px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 24px;
  font-weight: bold;
  color: #2d3436;
  cursor: pointer;
  transition: all 0.3s ease;
  padding: 8px 12px;
  border-radius: 8px;
}

.logo-icon {
  font-size: 18px;
  color: #667eea;
  transition: all 0.3s ease;
}

.logo:hover {
  color: #667eea;
  background: rgba(102, 126, 234, 0.1);
}

.logo:hover .logo-icon {
  transform: scale(1.1);
}

.logo span {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.search-box {
  display: flex;
  align-items: center;
  background: #f8f9fa;
  border-radius: 8px;
  padding: 8px 16px;
  flex: 1;
  max-width: 400px;
  margin: 0 20px;
}

.search-input {
  flex: 1;
  border: none;
  background: transparent;
  outline: none;
  font-size: 14px;
  color: #495057;
}

.search-input::placeholder {
  color: #adb5bd;
}

.search-btn {
  background: none;
  border: none;
  color: #adb5bd;
  cursor: pointer;
  padding: 4px;
  border-radius: 4px;
  transition: color 0.3s ease;
}

.search-btn:hover {
  color: #667eea;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.notification-icon {
  position: relative;
  cursor: pointer;
  padding: 8px;
  border-radius: 6px;
  transition: all 0.3s ease;
  color: #636e72;
}

.notification-icon:hover {
  background: #f8f9fa;
  color: #667eea;
}

.notification-badge {
  position: absolute;
  top: 4px;
  right: 4px;
  background: #e74c3c;
  color: white;
  font-size: 10px;
  padding: 2px 4px;
  border-radius: 8px;
  min-width: 16px;
  text-align: center;
}

.user-menu {
  position: relative;
  cursor: pointer;
}

.user-avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 16px;
  transition: all 0.3s ease;
}

.user-avatar:hover {
  transform: scale(1.05);
}

.user-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  min-width: 160px;
  z-index: 1000;
  margin-top: 8px;
  overflow: hidden;
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  color: #495057;
  text-decoration: none;
  transition: all 0.3s ease;
  border-bottom: 1px solid #f8f9fa;
}

.dropdown-item:last-child {
  border-bottom: none;
}

.dropdown-item:hover {
  background: #f8f9fa;
  color: #667eea;
}

.dropdown-item i {
  width: 16px;
  font-size: 14px;
}

.order-btn {
  padding: 8px 16px;
  font-size: 13px;
  border-radius: 6px;
  transition: all 0.3s ease;
}

.order-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.notification-content {
  padding: 0;
}

.notification-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 16px;
  border-radius: 8px;
  background: #f8f9fa;
}

.notification-item .notification-icon {
  color: #667eea;
  font-size: 18px;
}

.notification-text p {
  margin: 0;
  font-weight: 500;
  color: #2d3436;
}

.notification-time {
  font-size: 12px;
  color: #95a5a6;
}

@media (max-width: 768px) {
  .header-content {
    flex-direction: column;
    height: auto;
    padding: 15px 0;
    gap: 15px;
  }
  
  .logo {
    padding: 6px 10px;
    font-size: 20px;
  }
  
  .logo-icon {
    font-size: 16px;
  }
  
  .search-box {
    margin: 0;
    max-width: none;
  }
  
  .header-actions {
    gap: 8px;
  }
  
  .order-btn {
    padding: 6px 12px;
    font-size: 12px;
  }
}
</style> 